package com.joyxy.easy.monitor;

import cn.hutool.log.Log;
import cn.sanenen.dm.base.DmApi;
import lombok.Getter;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@Getter
public class StatusMonitor extends Thread {
    private static final Log log = Log.get();
    private long hwnd;
    private long baseAddress;
    private long debuffAddress;
    private volatile AtomicBoolean move = new AtomicBoolean(false);
    private boolean sell;
    private static volatile StatusMonitor instance;
    private double hp = -1;
    private double mp = -1;
    private String roleName;
    private long x = -1;
    private long y = -1;
    private long face; //0左 1右
    private long debuff;
    private long map;
    // 属性偏移量（16进制表示）
    private static final int HP_OFFSET = 0x088;    // 生命值
    private static final int MP_OFFSET = 0x098;    // 魔法值
    // 其他属性
    public static final int NAME_OFFSET = 0x070;  // 角色名称

    public static final int DX_OFFSET = 0x008;    // X坐标
    public static final int DY_OFFSET = 0x00C;    // Y坐标
    public static final int FACE_OFFSET = 0x058;  // 朝向
    public static final int MAP_OFFSET = 0x024;  // 地图

    public static final int DEBUFF_OFFSET = 0x020;
    private DmApi dmApi;

    public StatusMonitor(long hwnd, DmApi dmApi) {
        this.hwnd = hwnd;
        this.dmApi = dmApi;
    }

    @Override
    public void run() {
        long moduleBase = dmApi.GetModuleBaseAddr(hwnd, "client.exe");
//        log.info("clientModel=" + moduleBase);
        // 2. 第一级偏移：client.exe + 00291348
        long firstLevel = moduleBase + 0x00291348;
        long buffFirstLevel = moduleBase + 0x00297668;

        // 3. 读取第二级地址
        baseAddress = dmApi.ReadInt(hwnd, Long.toHexString(firstLevel), 4);


//        log.info("firstLevel=" + firstLevel + ", secondLevel=" + baseAddress);
        while (!Thread.currentThread().isInterrupted()) {
            try {
                debuffAddress = dmApi.ReadInt(hwnd, Long.toHexString(buffFirstLevel), 4);
                //读取最终偏移量的值
                hp = dmApi.ReadDouble(hwnd, Long.toHexString(baseAddress + HP_OFFSET));
//                log.info("当前血量为：" + hp);
                mp = dmApi.ReadDouble(hwnd, Long.toHexString(baseAddress + MP_OFFSET));
//                log.info("当前MP为:" + mp);
                x = dmApi.ReadInt(hwnd, Long.toHexString(baseAddress + DX_OFFSET), 1);
//                log.info("当前坐标x:" + x);
                y = dmApi.ReadInt(hwnd, Long.toHexString(baseAddress + DY_OFFSET), 1);
//                log.info("当前坐标y:" + y);
                face = dmApi.ReadInt(hwnd, Long.toHexString(baseAddress + FACE_OFFSET), 1);
//                log.info("当前朝向 " + face);
                map = dmApi.ReadInt(hwnd, Long.toHexString(baseAddress + MAP_OFFSET), 1);
//                log.info("当前地图 " + map);

                debuff = dmApi.ReadInt(hwnd, Long.toHexString(debuffAddress + DEBUFF_OFFSET), 1);

//                log.info("当前debuff " + debuff);
//                log.info("______________________");
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (Exception e) {
                log.error(e);
            }
        }
    }

    public boolean isMove() {
        return move.get();
    }

    public void setMove(boolean move) {
        this.move = new AtomicBoolean(move);
    }

    public boolean isSell() {
        return sell;
    }

    public void setSell(boolean sell) {
        this.sell = sell;
    }


    public long nowX() {
        x = dmApi.ReadInt(hwnd, Long.toHexString(baseAddress + DX_OFFSET), 1);
        return x;
    }

    public long nowFace() {
        face = dmApi.ReadInt(hwnd, Long.toHexString(baseAddress + FACE_OFFSET), 1);
        return face;
    }

}
